package com.example.emb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.emb.entity.Groupdtable;
import com.example.emb.entity.Grouptable;
import com.example.emb.entity.Usrtable;
import com.example.emb.mapper.GroupdtableMapper;
import com.example.emb.mapper.GrouptableMapper;
import com.example.emb.service.TeacherImpGroupService;
import com.example.emb.service.exception.MemberExistException;
import com.example.emb.until.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.jsqlparser.util.deparser.GroupByDeParser;
import org.apache.catalina.Group;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Api(tags = "教师导入小组info和小组管理 未更新")
@RequestMapping("/zjgsEmb/TeacherImGro")
public class TeacherGroController {
    @Autowired
    TeacherImpGroupService teacherImpGroupService;
    @Autowired
    GrouptableMapper grouptableMapper;
    @Autowired
    GroupdtableMapper groupdtableMapper;

    @ApiOperation("批量导入小组info")
    @PostMapping("ImportGro")
    private JsonResult ImportGro(@RequestParam("file") MultipartFile file,int classId,int TeachingClassId){

        if (!file.getContentType().equals("application/vnd.ms-excel") && !file.getContentType().equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
            return new JsonResult(10001,"上传非excel文件");
        }
        //如果操作失败了，应该要有这里有自动回滚和手动回滚的方法
        teacherImpGroupService.ImportGroupByExcel(file,classId,TeachingClassId);

        return new JsonResult<>(200);
    }
    //如果操作失败了，应该要有这里有自动回滚和手动回滚的方法

    @ApiOperation("手动导入小组info")
    @PostMapping("InsertGro")
    private JsonResult<String > InsertGro(Grouptable gro, List<Usrtable> list,int TeachingClassId){
        teacherImpGroupService.InitGroup(gro.getGroupName(),gro.getYear(), gro.getClassId(), list,TeachingClassId);
        return new JsonResult(200);
    }
    @ApiOperation("修改小组信息")
    @PutMapping("/ChangeGro")
    private JsonResult ChangeGro(Grouptable group,List<Usrtable> listInsert,List<Usrtable> listDelete){
        try {
            teacherImpGroupService.updateGroup(group.getId(),listInsert,listDelete);
        }catch (MemberExistException e){
            return new JsonResult<String>(506,e.getMessage()+"操作已经回滚，请选择正确的人再次操作");
        }
        return new JsonResult(200);
    }

    @ApiOperation("手动删除小组info")
    @PostMapping("DeleteGro")
    private JsonResult DeleteGro(String GroupId){
        QueryWrapper<Groupdtable> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("GroupId",GroupId);
        List<Groupdtable>list=groupdtableMapper.selectList(queryWrapper);
        for (Groupdtable groupdtable:list){
            groupdtableMapper.deleteById(groupdtable.getId());
        }
        grouptableMapper.deleteById(GroupId);
        return new JsonResult(200);
    }

    @ApiOperation("查询小组info")
    @PostMapping("SearchGro")
    private JsonResult<List<Grouptable>> SearchStu(Grouptable gro){
//    复用接口
        List<Grouptable> list=teacherImpGroupService.selectGro(gro);
        return new JsonResult<List<Grouptable>>(200,list);
    }

}
